package com.google.code.lightsomy.http;

import java.util.Collections;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

/**
 * Wrapper of {@link HttpServletRequest}.
 * 
 * Allows to modify default parameters within default visibility.
 * 
 * @author Viktoras Agejevas
 *
 */
public class LightsomyRequest extends HttpServletRequestWrapper {

	protected Map<String, String[]> params;
	
	@SuppressWarnings("unchecked")
	public LightsomyRequest(HttpServletRequest req) {
		super(req);
		params = new Hashtable<String, String[]>(req.getParameterMap());
	}

	void putParameter(String key, String[] value) {
		params.put(key, value);
	}
	
	void putParameter(String key, String value) {
		params.put(key, new String[] {value});
	}
	
	void putAllParameters(Map<String, String[]> parameters) {
		parameters.putAll(parameters);
	}

	void putAllStringParameters(Map<String, String> parameters) {
		for (Map.Entry<String, String> entry : parameters.entrySet()) {
			putParameter(entry.getKey(), entry.getValue());
		}
	}
	
	public String getParameter(String arg0) {
		if (params.containsKey(arg0) && params.get(arg0) instanceof String[]) {
			return params.get(arg0)[0];
		}
		return null;
	}

	public Map<String, String[]> getParameterMap() {
		return Collections.unmodifiableMap(new Hashtable<String, String[]>(params));
	}

	public Enumeration<String> getParameterNames() {
		return ((Hashtable<String, String[]>)params).keys();
	}

	public String[] getParameterValues(String arg0) {
		return params.get(arg0);
	}
}
